﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PlutoStudio
{
    public static class MiscExtensions
    {
        /// <summary>
        /// 根据指定的条件决定是否需要执行指定的动作。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="condition"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static T If<T>(this T source, bool condition, Action<T> action)
        {
            if (condition)
            {
                action(source);
            }
            return source;
        }
        /// <summary>
        /// 根据指定的条件决定是否需要执行指定的方法并返回方法的返回值。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="condition"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        public static TResult If<T,TResult>(this T source, bool condition, Func<T,TResult> func)
        {
            if (condition)
            {
                return func(source);
            }
            return default(TResult);
        }
    }
}
